//
//  IntroductionViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "IntroductionViewController.h"
#import "IntroductionView.h"

@interface IntroductionViewController ()<UITextViewDelegate>

@property(nonatomic, strong) IntroductionView *introductionView;

@end

@implementation IntroductionViewController

- (void)loadView
{
    self.introductionView = [[IntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.view = self.introductionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor RGBColor:239 green:239 blue:244 alpha:1]; // 背景颜色
    
    [self loadNavigationIndfo]; // 设置导航栏
    
    [self loadKeyBoard]; // 给键盘添加按钮
    
    [self loadTextView]; // 设置TextView的相关操作
}

#pragma mark - 设置导航栏
- (void)loadNavigationIndfo
{
    // 导航栏透明度
    self.navigationController.navigationBar.translucent = NO;
    
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
    
    // 导航栏标题
    self.navigationItem.title = @"一句话简介";
    
    // 返回按钮
    UIBarButtonItem *leftBackButton = [UIBarButtonItem itemWithTarget:self Action:@selector(backAction) image:@"home_back" highImage:@"home_back_highlighted"];
    self.navigationItem.leftBarButtonItem = leftBackButton;
    
    // 保存按钮缪
    UIBarButtonItem *rightSaveButton  = [UIBarButtonItem itemWithTarget:self Action:@selector(saveAction) title:@"保存"];
    self.navigationItem.rightBarButtonItem = rightSaveButton;
    
    // 标题颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

#pragma mark - 设置textView的相关操作
- (void)loadTextView
{
    self.introductionView.contentTextView.text = self.userIntroduction; //简介
    
    self.introductionView.countLabel.text = [NSString stringWithFormat:@"%ld/100",_userIntroduction.length]; //计算文本的长度
    
    self.introductionView.contentTextView.delegate = self;

}

#pragma mark - 返回按钮触发事件
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 保存
- (void)saveAction
{
    // block传值
    if (self.introductionBloak != nil) {
        self.introductionBloak(self.introductionView.contentTextView.text);
    }
    
    // 调用返回触发事件
    [self backAction];
}

#pragma mark - 自定义键盘
- (void)loadKeyBoard
{
    //定义一个toolBar
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    
    //设置style
    [topView setBarStyle:UIBarStyleBlack];
    
    //定义两个flexibleSpace的button，放在toolBar上，这样完成按钮就会在最右边
    UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem * button2 = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    //定义完成按钮
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone  target:self action:@selector(resignKeyboard)];
    
    //在toolBar上加上这些按钮
    NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
    [topView setItems:buttonsArray];
    
    [self.introductionView.contentTextView setInputAccessoryView:topView];
}

#pragma mark - 隐藏键盘
- (void)resignKeyboard {
    [self.introductionView.contentTextView resignFirstResponder];
}

#pragma mark - 点击空白处键盘回收
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    
    if (![touch.view isKindOfClass: [UITextField class]] || ![touch.view isKindOfClass: [UITextView class]]) {
        
        [self.view endEditing:YES];
        
    }
}

#pragma mark - 代理方法
-(void)textViewDidChange:(UITextView *)textView
{
    [self textViewChange:textView]; //iOS7光标问题
    if (textView.text.length <= 100)
    {
        self.introductionView.countLabel.text = [NSString stringWithFormat:@"%ld/100",textView.text.length]; //计算文本的长度
    }
    else
    {
        self.introductionView.countLabel.text = @"100/100";
        NSRange rang = NSMakeRange(0, 30);
        // 截取20个字符
        self.introductionView.contentTextView.text = [textView.text substringWithRange:rang];
    }
    
}


#pragma mark - iOS7光标问题
//遇到textView在ios7上出现编辑进入最后一行时光标消失，看不到最后一行，变成盲打，stackOverFlow网站上有大神指出，是ios7本身bug，加上下面一段代码即可
- (void)textViewChange:(UITextView *)textView
{
    CGRect line = [textView caretRectForPosition:
                   textView.selectedTextRange.start];
    CGFloat overflow = line.origin.y + line.size.height
    - ( textView.contentOffset.y + textView.bounds.size.height
       - textView.contentInset.bottom - textView.contentInset.top );
    if ( overflow > 0 ) {
        // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
        // Scroll caret to visible area
        CGPoint offset = textView.contentOffset;
        offset.y += overflow + 7; // leave 7 pixels margin
        // Cannot animate with setContentOffset:animated: or caret will not appear
        [UIView animateWithDuration:.2 animations:^{
            [textView setContentOffset:offset];
        }];
    }
}

#pragma mark - 内容警告触发方法
- (void)didReceiveMemoryWarning
{
    [[SDImageCache sharedImageCache] clearMemory];
}


@end
